/*
 * Copyright 2017-8-30 13:56:26 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.ao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.validation.BindingResult;

import com.lap.scd.commons.ao.AbstractAO;
import com.lap.scd.commons.constant.RespCode;
import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.page.PageUtils;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.admin.ao.UserAO;
import com.lap.scp.admin.bean.UserBean;
import com.lap.scp.admin.core.LabelManager;
import com.lap.scp.admin.core.ResourcesManager;
import com.lap.scp.admin.core.UserManager;
import com.lap.scp.admin.domain.LabelDO;
import com.lap.scp.admin.domain.UserDO;
import com.lap.scp.admin.domain.UserRoleDO;
import com.lap.scp.admin.query.UserQueryDO;
import com.lap.scp.admin.utils.ListUtils;

/**
 * <pre>
 * 管理员应用对象
 * </pre>
 * 
 * @author 系统自动生成 Exp
 * @Date 2017-8-30 13:56:26
 * @since 1.0
 */
@Service("userAO")
public class DefaultUserAO extends AbstractAO implements UserAO {

	@Resource
	private UserManager userManager;

	@Resource
	private LabelManager labelManager;

	@Resource
	private ResourcesManager resourcesManager;

	@Override
	public Resp<Integer> deleteById(int userId) {
		Resp<Integer> resp = new Resp<>();
		try {
			int flag = userManager.deleteById(userId);
			resp.setData(flag);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(userId, e);
		}
		return resp;
	}

	@Override
	public Resp<UserDO> insertUser(UserDO userDO, BindingResult error) {
		Resp<UserDO> resp = new Resp<>();
		try {
			super.validate(resp, error, userDO);
			if (resp.isSuccess()) {

				UserDO queryUser = userManager.queryByUsername(userDO.getUsername());
				if (queryUser != null) {
					resp.putMsg(getMsg("LG00004"));
				} else {
					String md5 = DigestUtils.md5DigestAsHex(userDO.getPasswd().getBytes());
					userDO.setPasswd(md5);
					userManager.insertUser(userDO);
					resp.setData(userDO);
				}
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(userDO, e);
		}
		return resp;
	}

	@Override
	public Resp<UserDO> queryById(int userId) {
		Resp<UserDO> resp = new Resp<>();
		try {
			UserDO userDO = userManager.queryById(userId);
			resp.setData(userDO);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(userId, e);
		}
		return resp;
	}

	@Override
	public PageResult<UserDO> queryPage(Page page, UserQueryDO userQueryDO) {
		PageResult<UserDO> pr = new PageResult<UserDO>();
		try {
			List<UserDO> list = userManager.queryPage(page, userQueryDO);

			PageUtils.copyPage(pr, page);
			pr.setData(list);
		} catch (Exception e) {
			pr.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(new Object[] { page, userQueryDO }, e);
		}
		return pr;
	}

	@Override
	public Resp<Integer> updateUser(UserDO userDO, BindingResult error) {
		Resp<Integer> resp = new Resp<>();
		try {
			super.validate(resp, error, userDO);
			if (resp.isSuccess()) {
				int flag = userManager.updateUser(userDO);
				resp.setData(flag);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(userDO, e);
		}
		return resp;
	}

	@Override
	public Resp<Integer> updateStatus(int userId, int status, String updatedBy) {
		Resp<Integer> resp = new Resp<>();
		try {
			int flag = userManager.updateStatus(userId, status, updatedBy);
			resp.setData(flag);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(userId, e);
		}
		return resp;
	}

	@Override
	public Resp<Integer> updatePasswd(int userId, String oldPd, String newPd, String lastUpdateBy) {
		Resp<Integer> resp = new Resp<>();
		try {
			UserDO queryDO = userManager.queryById(userId);
			if (queryDO == null) {
				resp.putMsg(getMsg("LG00001"));
			} else {
				String queryPd = queryDO.getPasswd();
				String oldPdMd5 = DigestUtils.md5DigestAsHex(oldPd.getBytes());
				if (!queryPd.equals(oldPdMd5)) {
					resp.putMsg(getMsg("LG00002"));
				} else {
					int flag = userManager.updatePasswd(userId, newPd, lastUpdateBy);
					resp.setData(flag);
				}
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(new Object[] { userId, oldPd, newPd, lastUpdateBy }, e);
		}
		return resp;
	}

	@Override
	public Resp<UserBean> queryForBind(int userId) {
		Resp<UserBean> resp = new Resp<>();
		try {
			UserBean bean = new UserBean();
			UserDO userDO = userManager.queryById(userId);
			if (userDO != null) {
				bean.setUserDO(userDO);

				List<Integer> roleIdList = userManager.queryRoleIdById(userId);
				bean.setRoleIdList(roleIdList);

				List<LabelDO> roleList = labelManager.queryRole();
				bean.setRoleList(roleList);
			}
			resp.setData(bean);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(userId, e);
		}
		return resp;
	}

	@Override
	public Resp<Integer> roleBind(int userId, String roleIds) {
		Resp<Integer> resp = new Resp<>();
		try {
			List<Integer> roleIdList = ListUtils.strToIntList(roleIds);

			List<UserRoleDO> userRoleList = new ArrayList<UserRoleDO>();
			UserRoleDO ur = null;
			for (Integer roleId : roleIdList) {
				ur = new UserRoleDO(roleId, userId);
				userRoleList.add(ur);
			}
			userManager.bindRole(userId, userRoleList);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(new Object[] { userId, roleIds }, e);
		}
		return resp;
	}

	@Override
	public Resp<UserDO> queryByUsername(String username) {
		Resp<UserDO> resp = new Resp<>();
		try {
			UserDO userDO = userManager.queryByUsername(username);
			resp.setData(userDO);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(username, e);
		}
		return resp;
	}

}
